# --*--coding=utf-8 --*--
"""
封板常用的数据类型转换方法
"""
import json
import re


def str_to_dict(s):
    """
    字符串内容是dict格式转换成dict类型
    :param s:
    :return:
    """
    if not isinstance(s, str):
        return
    s = re.sub('\'', '\"', s)
    if len(s) == 0:
        return
    return json.loads(s)


def bytes_to_dict(b):
    """
    字节类型变成字典
    :param b:
    :return:
    """
    if not isinstance(b, bytes):
        return b
    return eval(str(b, encoding='utf-8'))


def upd_dict_data(dict_data, upd_dict={}):
    """
    更新dict中指定健的值,只更新非列表的值
    :param dict_data:
    :param upd_dict:
    """
    if len(upd_dict) == 0 or upd_dict is None:
        return dict_data
    if not isinstance(dict_data, dict):
        return dict_data
    for upd_k, upd_v in upd_dict.items():
        for k, v in dict_data.items():
            if upd_k == k:
                dict_data[upd_k] = upd_v
            elif isinstance(v, list):
                for item in v:
                    upd_dict_data(item, upd_dict)
    return dict_data


if __name__ == '__main__':
    pass
